package cn.yeziji.forum.common;

import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.Objects;

/**
 * 权限用户
 *
 * @author gzkemays
 * @since 2021/12/21 16:23
 */
@Data
public class AuthUser implements UserDetails {
  private Integer id;

  /** 头像 */
  private String avatar;

  /** */
  private Date createTime;

  /** */
  private Boolean delete;

  /** 个人简介 */
  private String desc;

  /** 等级 */
  private String level;

  private String email;

  /** 昵称 */
  private String nickname;

  /** security encode */
  private String password;

  /** */
  private Date updateTime;

  /** 验证码 */
  private String code;

  /** 用户名 */
  private String username;
  // userDetails 必备属性
  private boolean enabled;
  private boolean accountNonExpired;
  private boolean accountNonLocked;
  private boolean credentialsNonExpired;

  @Override
  public Collection<? extends GrantedAuthority> getAuthorities() {
    return new ArrayList<>();
  }

  /**
   * 账户是否未过期
   *
   * @return true
   */
  @Override
  public boolean isAccountNonExpired() {
    return true;
  }

  /**
   * 账户是否未被锁定
   *
   * @return true
   */
  @Override
  public boolean isAccountNonLocked() {
    return true;
  }

  /**
   * 用户权限是否未过期
   *
   * @return true
   */
  @Override
  public boolean isCredentialsNonExpired() {
    return true;
  }

  /**
   * 是否启用
   *
   * @return true
   */
  @Override
  public boolean isEnabled() {
    return Objects.isNull(delete) || !delete;
  }
}
